// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境

// 云函数入口函数
exports.main = async (event, context) => {
    const db = cloud.database();
    event.repairForm._id = `${event.repairForm.sid}${new Date().getTime()}`;
    for (let item of event.fileIDs) {
        let ss = (await db.collection('repairImage').add({
            data: { orderID: event.repairForm._id, path: item }
        })).errMsg === 'collection.add:ok';
        if (!ss) return false;
    }
    const addedUOR = (await db.collection('UOR').add({
        data: {
            repairmanID: '',
            studentID: event.repairForm.sid,
            orderID: event.repairForm._id,
            finished: false
        }
    })).errMsg === 'collection.add:ok';
    return addedUOR && (await db.collection('repairOrder').add({
        data: event.repairForm
    })).errMsg === 'collection.add:ok';
}